﻿#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

struct TreeNode {
    int val;
    TreeNode* left;
    TreeNode* right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode* left, TreeNode* right) : val(x), left(left), right(right) {}
};

class Solution {
private:
    const int inf = 0x3f3f3f3f;
    bool dfs(TreeNode* root, int lower, int upper) {
        if (lower >= upper) return false;
        if (root == nullptr) return true;
        if (!(lower < root->val && root->val < upper)) return false;
        bool ret = true;
        if (root->left != nullptr)
            ret &= dfs(root->left, lower, root->val);
        if (root->right != nullptr)
            ret &= dfs(root->right, root->val, upper);
        return ret;
    }
public:
    bool isValidBST(TreeNode* root) {
        return dfs(root, -inf, inf);
    }
};

int main() {
    Solution solution;
    //TreeNode* node = new TreeNode(0);
    TreeNode* node = new TreeNode(32, new TreeNode(26, new TreeNode(19, nullptr, new TreeNode(25)), nullptr), new TreeNode(47, nullptr, new TreeNode(56)));
    cout << solution.isValidBST(node) << endl;
    return 0;
}